package tt.dz.appserver.service.business.imp;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.util.StringUtils;


import tt.dz.appserver.dao.ext.ImagesDao;
import tt.dz.appserver.dao.ext.LabelDao;
import tt.dz.appserver.dao.ext.ShareCommentDao;
import tt.dz.appserver.dao.ext.SharesDao;
import tt.dz.appserver.dao.ext.ThumbUpDao;
import tt.dz.appserver.dao.ext.UserInfoDao;
import tt.dz.appserver.entity.model.ResultMappingEntity;
import tt.dz.appserver.entity.model.pub.Images;
import tt.dz.appserver.entity.model.pub.Label;
import tt.dz.appserver.entity.model.pub.ShareComment;
import tt.dz.appserver.entity.model.pub.Shares;
import tt.dz.appserver.entity.model.pub.ThumbUp;
import tt.dz.appserver.entity.model.user.UserInfo;
import tt.dz.appserver.entity.vo.ShareCommentInfoVo;
import tt.dz.appserver.entity.vo.ShareImageVo;
import tt.dz.appserver.entity.vo.ShareLabelVo;
import tt.dz.appserver.entity.vo.SharesVo;
import tt.dz.appserver.entity.vo.user.UserBaseVo;
import tt.dz.appserver.service.business.ShareService;
import tt.dz.appserver.util.Result;
import tt.dz.appserver.util.date.DateUtil;
import tt.dz.appserver.util.encoding.EncodingUtils;
import tt.dz.appserver.util.enums.ImageAssociatedType;
import tt.dz.appserver.util.enums.ImageType;
import tt.dz.appserver.util.enums.Response;
import tt.dz.appserver.util.enums.ThumbUpType;
import tt.dz.appserver.util.enums.Valid;

/**
 * 桩友圈业务实现
 * @author Administrator
 *
 */
public class ShareServiceImpl implements ShareService {
	
	/**桩友圈分享*/
	@Resource
	private SharesDao sharesDao;
	/**标签*/
	@Resource
	private LabelDao labelDao;
	/**用户信息*/
	@Resource
	private UserInfoDao userInfoDao;
	/**图片操作*/
	@Resource
	private ImagesDao imagesDao;
	/**桩友圈评论*/
	@Resource
	private ShareCommentDao shareCommentDao;
	/**点赞信息*/
	@Resource
	private ThumbUpDao thumbUpDao;
	private final static Logger LOG = Logger.getLogger("[桩友圈接口]:");
	
	@Override
	public ResultMappingEntity shares(String userId, int pageNo) {
		
		List<Shares> sharesList = null;
		
		if(StringUtils.isEmpty(userId.trim())){
			sharesList = sharesDao.findSharesList(pageNo);
		}
		else{
			sharesList = sharesDao.findSharesList(userId, pageNo);
		}
		
		//分享
		List<SharesVo> sharesVosList = new ArrayList<SharesVo>();
		
		for (Shares shares : sharesList) {

			//需要展示的分享信息
			SharesVo sharesVo = new SharesVo();
			sharesVo.setAreaCode(shares.getArea_code());
			sharesVo.setContent(shares.getContent());
			if(shares.getLatitude()!=null){
				sharesVo.setLatitude(shares.getLatitude().toString());
			}

			if(shares.getLongitude()!=null){
				sharesVo.setLongitude(shares.getLongitude().toString());
			}
			sharesVo.setShareAddr(shares.getShare_addr());
			sharesVo.setShareId(shares.getShare_id());
			sharesVo.setShareTime(DateUtil.getDateTime(shares.getShare_time()));
			sharesVo.setUserId(shares.getUser_id());
			LOG.info("share_id="+shares.getShare_id()+"userid="+shares.getUser_id());
			UserInfo userInfo = userInfoDao.findUserByUserId(shares.getUser_id());
			if(userInfo!=null){
				//分享的用户基本信息
				UserBaseVo userBaseVo = new UserBaseVo();
				userBaseVo.setUserId(shares.getUser_id());
				userBaseVo.setNickName(userInfo.getNickname());
				userBaseVo.setSex(userInfo.getSex().toString());
				userBaseVo.setSignature(userInfo.getSignature());
				//用户头像
				userBaseVo.setHeadImage(userInfo.getHead_path());
				sharesVo.setUserInfo(userBaseVo);
			}			
			//分享标签
			List<Label> labelsList = labelDao.findShareLabelBySharesId(shares.getShare_id());
			List<ShareLabelVo> shareLabelVos = new ArrayList<ShareLabelVo>();
			for (Label label : labelsList) {
				ShareLabelVo shareLabelVo = new ShareLabelVo();
				shareLabelVo.setLabelId(label.getLabel_id());
				shareLabelVo.setLabelName(label.getLabel_name());
				shareLabelVo.setLabelType(label.getLabel_type());
				shareLabelVos.add(shareLabelVo);
			}
			sharesVo.setLabels(shareLabelVos);

			//获取桩友圈的小图片
			List<Images> smallImagesList = imagesDao.findImagesListByImagesAssociatedTypeAndImageTypeAndAssociatedId(ImageAssociatedType.SHARES.getValue(),
					ImageType.SMALL.getValue(), shares.getShare_id());
			
			//获取桩友圈的中图
			List<Images> middleImagesList = imagesDao.findImagesListByImagesAssociatedTypeAndImageTypeAndAssociatedId(ImageAssociatedType.SHARES.getValue(),
					ImageType.MIDDLE.getValue(), shares.getShare_id());
			
			//获取桩友圈的大图
			List<Images> bigImagesList = imagesDao.findImagesListByImagesAssociatedTypeAndImageTypeAndAssociatedId(ImageAssociatedType.SHARES.getValue(),
					ImageType.BIG.getValue(), shares.getShare_id());
			
			//大图中图小图的数量是一样的
			int shareImagesCount = smallImagesList.size();
			
			List<ShareImageVo> shareImageVosList = new ArrayList<>();
			for (int i = 0; i < shareImagesCount; i++) {
				
				//获取当前序号的 小图 中图  大图
				Images smallImage = smallImagesList.get(i);
				Images middleImage = middleImagesList.get(i);
				Images bigImage = bigImagesList.get(i);
				
				//设置图片
				ShareImageVo shareImageVo = new ShareImageVo();
				shareImageVo.setSmallImage(smallImage.getImage_path());
				shareImageVo.setMiddleImage(middleImage.getImage_path());
				shareImageVo.setBigImage(bigImage.getImage_path());
				shareImageVosList.add(shareImageVo);
			}
			//设置本条信息的图片信息
			sharesVo.setImages(shareImageVosList);
			
			//查询评论信息
			List<ShareComment> shareComments = shareCommentDao.findShareCommentByShareId(shares.getShare_id());
			List<ShareCommentInfoVo> commentInfoVos = new ArrayList<ShareCommentInfoVo>();
			for (ShareComment shareComment : shareComments) {
				
				//封装用户的评论信息
				ShareCommentInfoVo shareCommentInfoVo = new ShareCommentInfoVo();
				shareCommentInfoVo.setCommentAddr(shareComment.getComment_addr());
				shareCommentInfoVo.setCreateTime(DateUtil.getDateTime(shareComment.getCreate_time()));
				shareCommentInfoVo.setContent(shareComment.getContent());
				
				//获取用户Id
				String userInfoId = shareComment.getUser_id();
				//评论用户信息
				UserInfo cUserInfo = userInfoDao.findUserByUserId(userInfoId);
				if(cUserInfo==null){
					LOG.error("shares:评论信息的发表用户信息丢失，用户id="+userInfoId);
					continue;
				}
				UserBaseVo commentUserInfo = new UserBaseVo();
				//用户头像
				commentUserInfo.setHeadImage(cUserInfo.getHead_path());
				commentUserInfo.setNickName(cUserInfo.getNickname());
				commentUserInfo.setSex(cUserInfo.getSex().toString());
				commentUserInfo.setSignature(cUserInfo.getSignature());
				commentUserInfo.setUserId(cUserInfo.getUser_id());
				shareCommentInfoVo.setCommentUserInfo(commentUserInfo);
				
				//被评论的用户Id
				String replyUserInfoId = shareComment.getReply_user_id();
				if(replyUserInfoId!=null){
					//被回复的用户信息
					UserInfo rUserInfo = userInfoDao.findUserByUserId(replyUserInfoId);
					
					if(rUserInfo!=null){
						UserBaseVo replyUserInfo = new UserBaseVo();
						//用户头像
						replyUserInfo.setHeadImage(rUserInfo.getHead_path());
						replyUserInfo.setNickName(rUserInfo.getNickname());
						replyUserInfo.setSex(rUserInfo.getSex().toString());
						replyUserInfo.setSignature(rUserInfo.getSignature());
						replyUserInfo.setUserId(rUserInfo.getUser_id());
						shareCommentInfoVo.setReplyUserInfo(replyUserInfo);
					}
				}
				
				//将本次view加入评论展示链表
				commentInfoVos.add(shareCommentInfoVo);
			}
			//设置评论信息
			sharesVo.setSharesCommentInfos(commentInfoVos);
			
			//获取分享的点赞信息
			List<ThumbUp> thumbs = thumbUpDao.findThumbUpByAssociatedTypeAndAssociatedId(ThumbUpType.SHARE.getValue(), shares.getShare_id());
			//分享用户信息链表
			List<UserBaseVo> thumbUpUserInfos = new ArrayList<UserBaseVo>();
			for (ThumbUp thumbUp : thumbs) {
				
				//评论用户信息
				UserInfo tUserInfo = userInfoDao.findUserByUserId(thumbUp.getUser_id());

				if(tUserInfo!=null){
					UserBaseVo thumbUpUserInfo = new UserBaseVo();
					
					//用户头像
					thumbUpUserInfo.setHeadImage(tUserInfo.getHead_path());
					thumbUpUserInfo.setNickName(tUserInfo.getNickname());
					thumbUpUserInfo.setSex(tUserInfo.getSex().toString());
					thumbUpUserInfo.setSignature(tUserInfo.getSignature());
					thumbUpUserInfo.setUserId(tUserInfo.getUser_id());
					thumbUpUserInfos.add(thumbUpUserInfo);
				}
			}
			
			//设置分享信息的点赞用户列表
			sharesVo.setThumbUpUserList(thumbUpUserInfos);;
			
			//本条信息加入展示集合
			sharesVosList.add(sharesVo);
		}
		return Result.responseBody(Response.SUCCESS.getCode(), Response.SUCCESS.getMsg(), sharesVosList);
	}
	
	@Override
	public ResultMappingEntity sharesDetail(String shareId) {

		Shares shares = sharesDao.findSharesDetailByShareId(shareId);

		//需要展示的分享信息
		SharesVo sharesVo = null;
		if(shares!=null){
			
			sharesVo = new SharesVo();
			sharesVo.setAreaCode(shares.getArea_code());
			sharesVo.setContent(shares.getContent());
			if(shares.getLatitude()!=null){
				sharesVo.setLatitude(shares.getLatitude().toString());
			}
			if(shares.getLongitude()!=null){
				sharesVo.setLongitude(shares.getLongitude().toString());
			}
			sharesVo.setShareAddr(shares.getShare_addr());
			sharesVo.setShareId(shares.getShare_id());
			sharesVo.setShareTime(DateUtil.getDateTime(shares.getShare_time()));
			sharesVo.setUserId(shares.getUser_id());
			
			UserInfo userInfo = userInfoDao.findUserByUserId(shares.getUser_id());
			//分享的用户基本信息
			UserBaseVo userBaseVo = new UserBaseVo();
			userBaseVo.setUserId(shares.getUser_id());
			userBaseVo.setNickName(userInfo.getNickname());
			userBaseVo.setSex(userInfo.getSex().toString());
			userBaseVo.setSignature(userInfo.getSignature());
			//用户头像
			userBaseVo.setHeadImage(userInfo.getHead_path());
			sharesVo.setUserInfo(userBaseVo);
			
			//分享标签
			List<Label> labelsList = labelDao.findShareLabelBySharesId(shares.getShare_id());
			List<ShareLabelVo> shareLabelVos = new ArrayList<ShareLabelVo>();
			for (Label label : labelsList) {
				ShareLabelVo shareLabelVo = new ShareLabelVo();
				shareLabelVo.setLabelId(label.getLabel_id());
				shareLabelVo.setLabelName(label.getLabel_name());
				shareLabelVo.setLabelType(label.getLabel_type());
				shareLabelVos.add(shareLabelVo);
			}
			sharesVo.setLabels(shareLabelVos);

			//获取桩友圈的小图片
			List<Images> smallImagesList = imagesDao.findImagesListByImagesAssociatedTypeAndImageTypeAndAssociatedId(ImageAssociatedType.SHARES.getValue(),
					ImageType.SMALL.getValue(), shares.getShare_id());
			
			//获取桩友圈的中图
			List<Images> middleImagesList = imagesDao.findImagesListByImagesAssociatedTypeAndImageTypeAndAssociatedId(ImageAssociatedType.SHARES.getValue(),
					ImageType.MIDDLE.getValue(), shares.getShare_id());
			
			//获取桩友圈的大图
			List<Images> bigImagesList = imagesDao.findImagesListByImagesAssociatedTypeAndImageTypeAndAssociatedId(ImageAssociatedType.SHARES.getValue(),
					ImageType.BIG.getValue(), shares.getShare_id());
			
			//大图中图小图的数量是一样的
			int shareImagesCount = smallImagesList.size();
			
			List<ShareImageVo> shareImageVosList = new ArrayList<>();
			for (int i = 0; i < shareImagesCount; i++) {
				
				//获取当前序号的 小图 中图  大图
				Images smallImage = smallImagesList.get(i);
				Images middleImage = middleImagesList.get(i);
				Images bigImage = bigImagesList.get(i);
				
				//设置图片
				ShareImageVo shareImageVo = new ShareImageVo();
				shareImageVo.setSmallImage(smallImage.getImage_path());
				shareImageVo.setMiddleImage(middleImage.getImage_path());
				shareImageVo.setBigImage(bigImage.getImage_path());
				shareImageVosList.add(shareImageVo);
			}
			//设置本条信息的图片信息
			sharesVo.setImages(shareImageVosList);
			
			//查询评论信息
			List<ShareComment> shareComments = shareCommentDao.findShareCommentByShareId(shares.getShare_id());
			List<ShareCommentInfoVo> commentInfoVos = new ArrayList<ShareCommentInfoVo>();
			for (int i=0;i< shareComments.size();i++) {
				ShareComment shareComment=shareComments.get(i);
				//封装用户的评论信息
				ShareCommentInfoVo shareCommentInfoVo = new ShareCommentInfoVo();
				shareCommentInfoVo.setCommentAddr(shareComment.getComment_addr());
				shareCommentInfoVo.setCreateTime(DateUtil.getDateTime(shareComment.getCreate_time()));
				shareCommentInfoVo.setContent(shareComment.getContent());
				
				//获取用户Id
				String userInfoId = shareComment.getUser_id();
				//评论用户信息
				UserInfo cUserInfo = userInfoDao.findUserByUserId(userInfoId);
				if(cUserInfo==null){
					LOG.error("评论信息的发表用户信息丢失，用户id="+userInfoId);
					continue;
				}
				UserBaseVo commentUserInfo = new UserBaseVo();
				//用户头像
				commentUserInfo.setHeadImage(cUserInfo.getHead_path());
				commentUserInfo.setNickName(cUserInfo.getNickname());
				commentUserInfo.setSex(cUserInfo.getSex().toString());
				commentUserInfo.setSignature(cUserInfo.getSignature());
				commentUserInfo.setUserId(cUserInfo.getUser_id());
				shareCommentInfoVo.setCommentUserInfo(commentUserInfo);
				
				//被评论的用户Id
				String replyUserInfoId = shareComment.getReply_user_id();
				if(replyUserInfoId!=null){
					//被回复的用户信息
					UserInfo rUserInfo = userInfoDao.findUserByUserId(replyUserInfoId);
					if(rUserInfo==null){
						LOG.error("评论信息被回复用户信息丢失，用户id="+replyUserInfoId);
						continue;
					}
					UserBaseVo replyUserInfo = new UserBaseVo();
					//用户头像
					replyUserInfo.setHeadImage(rUserInfo.getHead_path());
					replyUserInfo.setNickName(rUserInfo.getNickname());
					replyUserInfo.setSex(rUserInfo.getSex().toString());
					replyUserInfo.setSignature(rUserInfo.getSignature());
					replyUserInfo.setUserId(rUserInfo.getUser_id());
					shareCommentInfoVo.setReplyUserInfo(replyUserInfo);
				}
				
				//将本次view加入评论展示链表
				commentInfoVos.add(shareCommentInfoVo);
			}
			//设置评论信息
			sharesVo.setSharesCommentInfos(commentInfoVos);
			
			//获取分享的点赞信息
			List<ThumbUp> thumbs = thumbUpDao.findThumbUpByAssociatedTypeAndAssociatedId(ThumbUpType.SHARE.getValue(), shares.getShare_id());
			//分享用户信息链表
			List<UserBaseVo> thumbUpUserInfos = new ArrayList<UserBaseVo>();
			for (ThumbUp thumbUp : thumbs) {
				
				//评论用户信息
				UserInfo tUserInfo = userInfoDao.findUserByUserId(thumbUp.getUser_id());
				UserBaseVo thumbUpUserInfo = new UserBaseVo();
				
				//用户头像
				thumbUpUserInfo.setHeadImage(tUserInfo.getHead_path());
				thumbUpUserInfo.setNickName(tUserInfo.getNickname());
				thumbUpUserInfo.setSex(tUserInfo.getSex().toString());
				thumbUpUserInfo.setSignature(tUserInfo.getSignature());
				thumbUpUserInfo.setUserId(tUserInfo.getUser_id());
				thumbUpUserInfos.add(thumbUpUserInfo);
			}
			
			//设置分享信息的点赞用户列表
			sharesVo.setThumbUpUserList(thumbUpUserInfos);;
		}
		return Result.responseBody(Response.SUCCESS.getCode(), Response.SUCCESS.getMsg(), sharesVo);
	}

	@Override
	public ResultMappingEntity releaseShares(String userId, String content, String shareAddress, 
			String areaCode, String smallImage[], String middleImage[], String bigImage[]) {

		ResultMappingEntity resultMappingEntity = null;
		
		//取出三种图片数组的长度
		int smallImageLength = smallImage==null?0:smallImage.length;
		int middleImageLength = middleImage==null?0:middleImage.length;
		int bigImageLength = bigImage==null?0:bigImage.length;
		
		//三种图片长度必须保持一致
		if((smallImageLength != middleImageLength && smallImageLength != bigImageLength)){
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg());
		}
		else{
			
			Shares shares = new Shares();
			shares.setArea_code(areaCode);
			shares.setContent(content);
			shares.setShare_addr(shareAddress);
			shares.setShare_time(new Date());
			shares.setUser_id(userId);
			shares.setShare_id(DateUtil.getDateTimeLong()+userId+EncodingUtils.getRandomIntegerString(4));
			shares.setValid_flag(Valid.YES.getValue());
			
			//保存分享
			sharesDao.save(shares);
			
			for (int i = 0; i < smallImageLength; i++) {
				
				if(!StringUtils.isEmpty(smallImage[i]) && !StringUtils.isEmpty(smallImage[i]) && !StringUtils.isEmpty(smallImage[i])){
					
					Images images = new Images();
					images.setImage_serial_no(i+1);
					images.setAssociated_type(ImageAssociatedType.SHARES.getValue());
					images.setAssociated_id(shares.getShare_id());
					images.setValid_flag(1);
					
					//小图保存
					images.setImage_type(ImageType.SMALL.getValue());
					images.setImage_path(smallImage[i]);
					imagesDao.save(images);
					
					//保存中图
					images.setImage_type(ImageType.MIDDLE.getValue());
					images.setImage_path(middleImage[i]);
					imagesDao.save(images);
					
					//保存大图
					images.setImage_type(ImageType.BIG.getValue());
					images.setImage_path(bigImage[i]);
					imagesDao.save(images);
				}
			}
			resultMappingEntity = Result.responseBody(Response.SUCCESS.getCode(), Response.SUCCESS.getMsg());
		}
		return resultMappingEntity;
	}

	@Override
	public ResultMappingEntity shareThumbUp(String userId, String shareId) {
		
		ThumbUp thumbUp = thumbUpDao.findThumbUpByUserIdAndAssociatedTypeAndAssociatedId(userId,ThumbUpType.SHARE.getValue(), shareId);
		if(thumbUp==null){
			thumbUp = new ThumbUp();
			thumbUp.setAssociated_id(shareId);
			thumbUp.setAssociated_type(ThumbUpType.SHARE.getValue());
			thumbUp.setCreate_time(new Date());
			thumbUp.setUser_id(userId);
			thumbUpDao.save(thumbUp);
		}
		else{
			thumbUpDao.delete(thumbUp);
		}
		return Result.responseBody(Response.SUCCESS.getCode(), Response.SUCCESS.getMsg());
	}

	@Override
	public ResultMappingEntity shareComment(String userId, String shareId, String replyUserId, String content, String commentAddr) {
		
		ShareComment shareComment = new ShareComment();
		shareComment.setComment_addr(commentAddr);
		shareComment.setContent(content);
		shareComment.setCreate_time(new Date());
		shareComment.setReply_user_id(replyUserId);
		shareComment.setShare_id(shareId);
		shareComment.setUser_id(userId);
		shareComment.setValid_flag(1);
		shareComment.setComment_id(DateUtil.getDateTimeLong()+EncodingUtils.getRandomIntegerString(4));
		shareCommentDao.save(shareComment);
		return Result.responseBody(Response.SUCCESS.getCode(), Response.SUCCESS.getMsg());
	}

	@Override
	public ResultMappingEntity getRecommendLabel() {
		return null;
	}

	@Override
	public ResultMappingEntity deleteShares(String shareId) {
		
		Shares shares = new Shares();
		shares.setShare_id(shareId);
		sharesDao.LogicDelete(shares);
		return Result.responseBody(Response.SUCCESS.getCode(), Response.SUCCESS.getMsg());
	}

	@Override
	public ResultMappingEntity getAllLabel() {
		return null;
	}
}
